package cn.dengta.webapp.trade.service;

import java.math.BigDecimal;
import java.util.Date;

import cn.dengta.common.model.TimeInterval;
import cn.dengta.webapp.base.service.BaseServiceImpl;
import cn.dengta.webapp.trade.dao.UserIntegralDailyStatDao;
import cn.dengta.webapp.trade.entity.UserIntegralDailyStat;
import cn.dengta.webapp.trade.model.TradeType;
import me.codeplayer.util.EasyDate;
import org.apache.ibatis.cursor.Cursor;
import org.springframework.stereotype.Service;

/**
 * 用户积分每日汇总 服务实现类
 *
 * @author lizhichao
 * @since 2024-01-03
 */
@Service
public class UserIntegralDailyStatService extends BaseServiceImpl<UserIntegralDailyStat, UserIntegralDailyStatDao, Long> {

	public void stat(Long userId, TradeType coinTradeType, BigDecimal tradeAmount, Date now) {
		if (coinTradeType == null) {
			return;
		}

		String field;
		switch (coinTradeType) {
			case COIN_FOR_GIFT:
			case COIN_FOR_GIFT_LIVE:
			case COIN_FOR_GIFT_BAG:
				field = UserIntegralDailyStat.GIFT_INCOME;
				break;
			case COIN_FOR_AUDIO:
				field = UserIntegralDailyStat.AUDIO_INCOME;
				break;
			case COIN_FOR_VIDEO:
				field = UserIntegralDailyStat.VIDEO_INCOME;
				break;
			case COIN_FOR_GREET:
			case COIN_FOR_MSG:
				field = UserIntegralDailyStat.TEXT_INCOME;
				break;
			default:
				return;
		}

		final String date = EasyDate.toString(now);
		int rows = baseDao.tryAddIncome(userId, field, tradeAmount, date, now);
		//  尝试更新成功
		if (rows > 0) {
			return;
		}
		baseDao.addIncome(userId, field, tradeAmount, date, now);
	}

	public BigDecimal getTotalIncome(Long userId, TimeInterval timeInterval) {
		return baseDao.getTotalIncome(userId, timeInterval);
	}

	public Cursor<UserIntegralDailyStatDao.UserIntegralTotal> cursorIncome(TimeInterval interval, Integer incomeThreshold) {
		return baseDao.cursorIncome(interval, incomeThreshold);
	}

	public Cursor<UserIntegralDailyStatDao.UserIntegralTotal> cursorNewUserIncome(Date newUserTime, Date date, Integer incomeThreshold) {
		return baseDao.cursorNewUserIncome(newUserTime, date, incomeThreshold);
	}

	public Cursor<UserIntegralDailyStatDao.UserIntegralTotal> cursorOldUserIncome(Date newUserTime, Date date, Integer incomeThreshold) {
		return baseDao.cursorOldUserIncome(newUserTime, date, incomeThreshold);
	}

}
